import { createRouter, createWebHistory } from 'vue-router'
//import HomeView from '../views/HomeView.vue'
import login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
import patient from '../components/user/patient.vue'
import nurse from '../components/user/nurse.vue'
import project from '../components/project.vue'
import rights from '../components/power/rights.vue'
import roles from '../components/power/roles.vue'
//import cate from '../components/cate.vue'
import order from '../components/order.vue'
const routes = [
  {path: '/',redirect: '/login'},   //重定向到/login
  {path: '/login',component: login},
  {path: '/home',component: home ,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:welcome},
      {path:'/patient',component:patient},
      {path:'/nurse',component:nurse},
      {path:'/project',component:project},
      {path:'/rights',component:rights},
      {path:'/roles',component:roles},
      {path:'/order',component:order},
      
    ]
  },
  
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to表示将要访问的路径
  //from表示从哪个路径跳转过来
  //next是一个函数，表示放行,next('/login')表示强制跳转
  if(to.path==='/login') return next()
  //获取token
  const tokenStr= window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()

})

export default router
